//
//  LEHttp.m
//  YouTiaoMovie
//
//  Created by jun on 2019/6/19.
//  Copyright © 2019 lechen. All rights reserved.
//

#import "LEHttp.h"

@implementation LEHttp


+(void)httpJzdxPost:(NSString*)url token:(NSString*)token param:(NSDictionary*)param response:(YTHttpResponse)response{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil,nil];
    NSMutableDictionary *dict = [param mutableCopy];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"*/*" forHTTPHeaderField:@"Accept"];
    NSString *agent = @"okhttp/4.9.0";
    [manager.requestSerializer setValue:agent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
    
        
    if(!ISEmpty(token)){
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",token] forHTTPHeaderField:@"token"];
    }
    
    [manager POST:url parameters:dict headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = 400;
        NSString *message = @"";
        if(responseObject){
            code = [responseObject[@"code"] integerValue];
            message = responseObject[@"msg"];
        }
        if(response){
            response(code,message, responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(response){
            response(-1,@"Net error!",nil);
        }
    }];
}


+(void)httpPost:(NSString*)url token:(NSString*)token param:(NSDictionary*)param response:(YTHttpResponse)response{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil,nil];
    NSMutableDictionary *dict = [param mutableCopy];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSString *agent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 19_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.61(0x18003d2e) NetType/WIFI Language/zh_CN";
    [manager.requestSerializer setValue:agent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"https://servicewechat.com/wx1feeed1c356dc013/6/page-frame.html" forHTTPHeaderField:@"Referer"];
    
        
    if(!ISEmpty(token)){
        [manager.requestSerializer setValue:[NSString stringWithFormat:@"%@",token] forHTTPHeaderField:@"Authorization"];
    }
    
    [manager POST:url parameters:dict headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSInteger code = 400;
        NSString *message = @"";
        if(responseObject){
            code = [responseObject[@"code"] integerValue];
            message = responseObject[@"msg"];
        }
        if(response){
            response(code,message, responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(response){
            response(-1,@"网络错误",nil);
        }
    }];

    
    
}


+(void)httpUpJzdxImage:(NSString*)url img:(UIImage*)img key:(NSString*)key token:(NSString*)token response:(YTHttpResponse)response{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil,nil];
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer = requestSerializer;
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:@"*/*" forHTTPHeaderField:@"Accept"];
    NSString *agent = @"okhttp/4.9.0";
    [manager.requestSerializer setValue:agent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"keep-alive" forHTTPHeaderField:@"Connection"];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if(!ISEmpty(token)){
        [requestSerializer setValue:[NSString stringWithFormat:@"%@",token] forHTTPHeaderField:@"token"];
    }
   
    NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyyMMdd_HHmmss"];
                NSString *dateString = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString  stringWithFormat:@"MX_IMG_%@_1002.jpg", dateString];
                NSData *imageData = UIImageJPEGRepresentation(img, 0.5);
        [formData appendPartWithFileData:imageData name:key fileName:fileName mimeType:@"image/jpeg"];
                
                } error:nil];
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                  }
                  completionHandler:^(NSURLResponse * _Nonnull res, id  _Nullable responseObject, NSError * _Nullable error) {
                    NSLog(@"===res===%@",responseObject);
                      if (error) {
                          NSLog(@"Error: %@", error);
                          if(response){
                             response(-1,@"获取失败,检查网络",nil);
                         }
                      } else {
                          NSInteger code = 400;
                          NSString *message = @"";
                          if(responseObject){
                              code = [responseObject[@"code"] integerValue];
                              message = responseObject[@"msg"];
                          }
                          if(response){
                              response(code,message, responseObject);
                          }
                      }
                  }];

    [uploadTask resume];
}


+(void)httpUpImage:(NSString*)url img:(UIImage*)img key:(NSString*)key token:(NSString*)token response:(YTHttpResponse)response{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                         @"text/html",
                                                         @"image/jpeg",
                                                         @"image/png",
                                                         @"application/octet-stream",
                                                         @"text/json",
                                                         nil,nil];
    AFHTTPRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    NSString *agent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 19_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.61(0x18003d2e) NetType/WIFI Language/zh_CN";
    [manager.requestSerializer setValue:agent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:@"https://servicewechat.com/wx1feeed1c356dc013/6/page-frame.html" forHTTPHeaderField:@"Referer"];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    if(!ISEmpty(token)){
        [requestSerializer setValue:[NSString stringWithFormat:@"%@",token] forHTTPHeaderField:@"Authorization"];
    }
   
    NSMutableURLRequest *request = [requestSerializer multipartFormRequestWithMethod:@"POST" URLString:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                [formatter setDateFormat:@"yyyyMMddHHmmss"];
                NSString *dateString = [formatter stringFromDate:[NSDate date]];
                NSString *fileName = [NSString  stringWithFormat:@"%@.jpg", dateString];
                NSData *imageData = UIImageJPEGRepresentation(img, 0.5);
        [formData appendPartWithFileData:imageData name:key fileName:fileName mimeType:@"image/jpeg"];
                
                } error:nil];
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
                  uploadTaskWithStreamedRequest:request
                  progress:^(NSProgress * _Nonnull uploadProgress) {
                  }
                  completionHandler:^(NSURLResponse * _Nonnull res, id  _Nullable responseObject, NSError * _Nullable error) {
                    NSLog(@"===res===%@",responseObject);
                      if (error) {
                          NSLog(@"Error: %@", error);
                          if(response){
                             response(-1,@"获取失败,检查网络",nil);
                         }
                      } else {
                          NSInteger code = 400;
                          NSString *message = @"";
                          if(responseObject){
                              code = [responseObject[@"code"] integerValue];
                              message = responseObject[@"msg"];
                          }
                          if(response){
                              response(code,message, responseObject);
                          }
                      }
                  }];

    [uploadTask resume];
}

@end
